More InventoyUtil lore methods and PredicateStartsWith

This commit is contained in:
Magnus Ulf Jørgensen 2017-01-04 18:42:17 +01:00 committed by Olof Larsson
parent fcd97e3cf3
commit aeec67e052
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
3 changed files with 88 additions and 3 deletions

View File

@ -0,0 +1,33 @@
package com.massivecraft.massivecore.predicate;
public class PredicateStringStartsWith implements Predicate<String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String prefix;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public static PredicateStringStartsWith get(String prefix) { return new PredicateStringStartsWith(prefix); }
public PredicateStringStartsWith(String prefix)
{
if (prefix == null) throw new NullPointerException("prefix");
this.prefix = prefix;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(String str)
{
if (str == null) return false;
return str.startsWith(prefix);
}
}

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -32,6 +33,8 @@ import com.massivecraft.massivecore.comparator.ComparatorComparable;
import com.massivecraft.massivecore.comparator.ComparatorEntryValue; import com.massivecraft.massivecore.comparator.ComparatorEntryValue;
import com.massivecraft.massivecore.event.EventMassiveCoreLorePriority; import com.massivecraft.massivecore.event.EventMassiveCoreLorePriority;
import com.massivecraft.massivecore.mixin.MixinInventory; import com.massivecraft.massivecore.mixin.MixinInventory;
import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateStringStartsWith;
public class InventoryUtil public class InventoryUtil
{ {
@ -1252,4 +1255,52 @@ public class InventoryUtil
} }
} }
// -------------------------------------------- //
// LORE PREFIX
// -------------------------------------------- //
public static void removeLoreMatching(ItemStack item, Predicate<String> predicate)
{
if (predicate == null) throw new NullPointerException("prefix");
List<String> lore = getLore(item);
if (lore == null) return;
for (Iterator<String> it = lore.iterator(); it.hasNext();)
{
String line = it.next();
if (!predicate.apply(line)) continue;
it.remove();
}
setLore(item, lore);
}
public static void removeLoreWithPrefix(ItemStack item, String prefix)
{
removeLoreMatching(item, PredicateStringStartsWith.get(prefix));
}
public static List<String> getLoreMatching(ItemStack item, Predicate<String> predicate)
{
if (predicate == null) throw new NullPointerException("prefix");
List<String> lore = getLore(item);
if (lore == null) return null;
for (Iterator<String> it = lore.iterator(); it.hasNext();)
{
String line = it.next();
if (predicate.apply(line)) continue;
it.remove();
}
return lore;
}
public static List<String> getLoreWithPrefix(ItemStack item, String prefix)
{
return getLoreMatching(item, PredicateStringStartsWith.get(prefix));
}
} }

View File

@ -1,5 +1,7 @@
package com.massivecraft.massivecore.util; package com.massivecraft.massivecore.util;
import static com.massivecraft.massivecore.mson.Mson.mson;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -27,8 +29,6 @@ import com.massivecraft.massivecore.mson.MsonEvent;
import com.massivecraft.massivecore.predicate.Predicate; import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateStartsWithIgnoreCase; import com.massivecraft.massivecore.predicate.PredicateStartsWithIgnoreCase;
import static com.massivecraft.massivecore.mson.Mson.mson;
public class Txt public class Txt
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -43,6 +43,7 @@ public class Txt
public static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); public static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+");
public static final Pattern PATTERN_NEWLINE = Pattern.compile("\\r?\\n"); public static final Pattern PATTERN_NEWLINE = Pattern.compile("\\r?\\n");
public static final Pattern PATTERN_NUMBER = Pattern.compile("[0-9]+");
private static final Pattern PATTERN_UPPERCASE_ZEROWIDTH = Pattern.compile("(?=[A-Z])"); // NOTE: Use camelsplit instead for Java 6/7 compatibility. private static final Pattern PATTERN_UPPERCASE_ZEROWIDTH = Pattern.compile("(?=[A-Z])"); // NOTE: Use camelsplit instead for Java 6/7 compatibility.
public static final long millisPerSecond = 1000; public static final long millisPerSecond = 1000;