Skip to content

Commit

Permalink
Add support for data values in the blacklist.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Jul 28, 2014
1 parent 3aa8bbe commit 956d1b4
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 34 deletions.
44 changes: 11 additions & 33 deletions src/main/java/com/sk89q/worldguard/blacklist/Blacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldguard.blacklist.action.Action;
import com.sk89q.worldguard.blacklist.action.ActionType;
import com.sk89q.worldguard.blacklist.event.BlacklistEvent;
import com.sk89q.worldguard.blacklist.event.EventType;
import com.sk89q.worldguard.blacklist.target.WildcardDataMatcher;
import com.sk89q.worldguard.blacklist.target.TargetMatcher;
import com.sk89q.worldguard.blacklist.target.TargetMatcherParseException;
import com.sk89q.worldguard.blacklist.target.TargetMatcherParser;
import org.bukkit.ChatColor;

import java.io.BufferedReader;
Expand Down Expand Up @@ -135,6 +136,7 @@ public boolean check(BlacklistEvent event, boolean forceRepeat, boolean silent)
public void load(File file) throws IOException {
FileReader input = null;
MatcherIndex.Builder builder = new MatcherIndex.Builder();
TargetMatcherParser targetMatcherParser = new TargetMatcherParser();

try {
input = new FileReader(file);
Expand All @@ -157,29 +159,20 @@ public void load(File file) throws IOException {
currentEntries = new ArrayList<BlacklistEntry>();

for (String item : items) {
int id;

try {
id = Integer.parseInt(item.trim());
} catch (NumberFormatException e) {
id = getItemID(item.trim());
if (id == 0) {
logger.log(Level.WARNING, "Unknown block name: " + item);
break;
}
TargetMatcher matcher = targetMatcherParser.fromInput(item.trim());
BlacklistEntry entry = new BlacklistEntry(this);
builder.add(matcher, entry);
currentEntries.add(entry);
} catch (TargetMatcherParseException e) {
logger.log(Level.WARNING, "Could not parse a block/item heading: " + e.getMessage());
}

BlacklistEntry entry = new BlacklistEntry(this);

builder.add(new WildcardDataMatcher(id), entry);
currentEntries.add(entry);
}
} else if (currentEntries != null) {
String[] parts = line.split("=");

if (parts.length == 1) {
logger.log(Level.WARNING, "Found option with no value "
+ file.getName() + " for '" + line + "'");
logger.log(Level.WARNING, "Found option with no value " + file.getName() + " for '" + line + "'");
continue;
}

Expand Down Expand Up @@ -293,21 +286,6 @@ public void notify(BlacklistEvent event, String comment) {
*/
public abstract void broadcastNotification(String msg);

/**
* Get an item's ID from its name.
*
* @param name the name of the item to look up
* @return the id for name if contained in ItemId, else -1
*/
private static int getItemID(String name) {
ItemType type = ItemType.lookup(name);
if (type != null) {
return type.getID();
} else {
return -1;
}
}

public Cache<String, TrackedEvent> getRepeatingEventCache() {
return repeatingEventCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private MatcherIndex(Table<Integer, TargetMatcher, BlacklistEntry> entries) {
public List<BlacklistEntry> getEntries(Target target) {
List<BlacklistEntry> found = new ArrayList<BlacklistEntry>();
for (Entry<TargetMatcher, BlacklistEntry> entry : entries.row(target.getTypeId()).entrySet()) {
if (entry.getKey().matches(target)) {
if (entry.getKey().test(target)) {
found.add(entry.getValue());
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/sk89q/worldguard/blacklist/target/DataMask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

import com.google.common.base.Predicate;

/**
* Tests a data value.
*/
public interface DataMask extends Predicate<Short> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

import com.google.common.base.Predicate;

import static com.google.common.base.Preconditions.checkNotNull;

public class DataValueRangeMatcher implements TargetMatcher {

private final int typeId;
private final Predicate<Short> dataMatcher;

public DataValueRangeMatcher(int typeId, Predicate<Short> dataMatcher) {
checkNotNull(dataMatcher);
this.typeId = typeId;
this.dataMatcher = dataMatcher;
}

@Override
public int getMatchedTypeId() {
return typeId;
}

@Override
public boolean test(Target target) {
return typeId == target.getTypeId() && isDataInRange(target.getData());
}

private boolean isDataInRange(short data) {
return dataMatcher.apply(data);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

import com.sk89q.worldedit.blocks.ItemType;

public class MaterialTarget implements Target {

private int id;
private short data;

public MaterialTarget(int id, short data) {
this.id = id;
this.data = data;
}

@Override
public int getTypeId() {
return id;
}

@Override
public short getData() {
return data;
}

@Override
public String getFriendlyName() {
ItemType type = ItemType.fromID(id);
if (type != null) {
return type.getName() + " (#" + id + ":" + data + ")";
} else {
return "#" + id + ":" + data;
}
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/sk89q/worldguard/blacklist/target/RangeMask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

import com.google.common.collect.Range;

import static com.google.common.base.Preconditions.checkNotNull;

public class RangeMask implements DataMask {

private final Range<Short> range;

public RangeMask(Range<Short> range) {
checkNotNull(range);
this.range = range;
}

@Override
public boolean apply(Short data) {
return range.contains(data);
}

}
48 changes: 48 additions & 0 deletions src/main/java/com/sk89q/worldguard/blacklist/target/Target.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

/**
* A target is something that can have events attached to it.
*/
public interface Target {

/**
* Get the type ID.
*
* @return the type ID
*/
int getTypeId();

/**
* Get the data value.
*
* @return the data value
*/
short getData();

/**
* Get a friendly name to be printed.
*
* @return a friendly name
*/
String getFriendlyName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

/**
* Matches a {@link Target}.
*/
public interface TargetMatcher {

/**
* Get the matched type ID, which is merely used for indexing.
*
* @return the type ID
*/
int getMatchedTypeId();

/**
* Return whether the given target is matched by this matcher.
*
* @param target the target
* @return true if matched
*/
boolean test(Target target);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.blacklist.target;

import static com.google.common.base.Preconditions.checkNotNull;

public class TargetMatcherParseException extends Exception {

public TargetMatcherParseException(String message) {
super(checkNotNull(message));
}

}
Loading

1 comment on commit 956d1b4

@riddle
Copy link

@riddle riddle commented on 956d1b4 Aug 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my god, finally. Thank you so much!

Please sign in to comment.