Setup basic framework for lore sorting

This commit is contained in:
Magnus Ulf Jørgensen 2017-01-01 17:54:03 +01:00 committed by Olof Larsson
parent d69de79c84
commit fcd97e3cf3
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
6 changed files with 286 additions and 10 deletions

View File

@ -41,6 +41,7 @@ import com.massivecraft.massivecore.engine.EngineMassiveCoreCommandRegistration;
import com.massivecraft.massivecore.engine.EngineMassiveCoreDatabase;
import com.massivecraft.massivecore.engine.EngineMassiveCoreDestination;
import com.massivecraft.massivecore.engine.EngineMassiveCoreGank;
import com.massivecraft.massivecore.engine.EngineMassiveCoreLorePriority;
import com.massivecraft.massivecore.engine.EngineMassiveCoreMain;
import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerLeave;
import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerState;
@ -283,6 +284,7 @@ public class MassiveCore extends MassivePlugin
EngineMassiveCoreDatabase.class,
EngineMassiveCoreDestination.class,
EngineMassiveCoreGank.class,
EngineMassiveCoreLorePriority.class,
EngineMassiveCoreMain.class,
EngineMassiveCorePlayerLeave.class,
EngineMassiveCorePlayerState.class,

View File

@ -1,5 +1,12 @@
package com.massivecraft.massivecore;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bukkit.permissions.Permissible;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.command.editor.annotation.EditorName;
import com.massivecraft.massivecore.command.editor.annotation.EditorType;
import com.massivecraft.massivecore.command.type.TypeMillisDiff;
@ -9,11 +16,6 @@ import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.PermissionUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.xlib.mongodb.WriteConcern;
import org.bukkit.permissions.Permissible;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@EditorName("config")
public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
@ -159,4 +161,14 @@ public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
public boolean mcstatsEnabled = true;
// -------------------------------------------- //
// LORE SORTING
// -------------------------------------------- //
public boolean loreSortOnInventoryClick = false;
public boolean loreSortOnInventoryOpen = false;
public Map<String, Integer> lorePrioritiesPrefix = new MassiveMap<>();
public Map<String, Integer> lorePrioritiesRegex = new MassiveMap<>();
}

View File

@ -1,11 +1,10 @@
package com.massivecraft.massivecore.command.type.enumeration;
import java.util.Set;
import org.bukkit.Material;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.Material;
import java.util.Set;
public class TypeMaterial extends TypeEnum<Material>
{

View File

@ -0,0 +1,88 @@
package com.massivecraft.massivecore.engine;
import java.util.Map.Entry;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.event.EventMassiveCoreLorePriority;
import com.massivecraft.massivecore.util.InventoryUtil;
import com.massivecraft.massivecore.util.MUtil;
public class EngineMassiveCoreLorePriority extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineMassiveCoreLorePriority i = new EngineMassiveCoreLorePriority();
public static EngineMassiveCoreLorePriority get() { return i; }
// -------------------------------------------- //
// SORT LORE
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void sortItemLore(InventoryClickEvent event)
{
if (!MassiveCoreMConf.get().loreSortOnInventoryClick) return;
HumanEntity human = event.getWhoClicked();
if (MUtil.isntPlayer(human)) return;
ItemStack item = event.getCurrentItem();
if (InventoryUtil.isNothing(item)) return;
InventoryUtil.sortLore(item);
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void sortItemLore(InventoryOpenEvent event)
{
if (!MassiveCoreMConf.get().loreSortOnInventoryOpen) return;
HumanEntity human = event.getPlayer();
if (MUtil.isntPlayer(human)) return;
InventoryUtil.sortLore(event.getView().getTopInventory());
InventoryUtil.sortLore(event.getView().getBottomInventory());
}
// -------------------------------------------- //
// CONFIG PRIORITIES
// -------------------------------------------- //
@EventHandler(priority = EventPriority.HIGHEST)
public void setLorePriorities(EventMassiveCoreLorePriority event)
{
this.setPrioritiesPrefix(event);
this.setPrioritiesRegex(event);
}
public void setPrioritiesRegex(EventMassiveCoreLorePriority event)
{
for (Entry<String, Integer> prefixEntry : MassiveCoreMConf.get().lorePrioritiesRegex.entrySet())
{
String regex = prefixEntry.getKey();
int priority = prefixEntry.getValue();
event.setPriorityByRegex(regex, priority);
}
}
public void setPrioritiesPrefix(EventMassiveCoreLorePriority event)
{
for (Entry<String, Integer> prefixEntry : MassiveCoreMConf.get().lorePrioritiesPrefix.entrySet())
{
String prefix = prefixEntry.getKey();
int priority = prefixEntry.getValue();
event.setPriorityByPrefix(prefix, priority);
}
}
}

View File

@ -0,0 +1,117 @@
package com.massivecraft.massivecore.event;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.predicate.Predicate;
public class EventMassiveCoreLorePriority extends EventMassiveCore
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static int PRIORITY_DEFAULT = 0;
// Any lore added by a MassiveX plugin should start with this string and then be followed by its own unique sequence of characters.
public static String LORE_PREFIX = ChatColor.BLACK.toString() + ChatColor.GOLD + ChatColor.WHITE;
// -------------------------------------------- //
// REQUIRED EVENT CODE
// -------------------------------------------- //
private static final HandlerList handlers = new HandlerList();
@Override public HandlerList getHandlers() { return handlers; }
public static HandlerList getHandlerList() { return handlers; }
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final ItemStack item;
public ItemStack getItem() { return this.item; }
private final List<Entry<String, Integer>> lore;
public List<Entry<String, Integer>> getLore() { return this.lore; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public EventMassiveCoreLorePriority(ItemStack item)
{
if (item == null) throw new NullPointerException("item");
this.item = item;
this.lore = getLoreEntries(item);
}
private static List<Entry<String, Integer>> getLoreEntries(ItemStack item)
{
ItemMeta meta = item.getItemMeta();
if ( ! meta.hasLore()) return Collections.emptyList();
List<Entry<String, Integer>> ret = new MassiveList<>();
for (String line : meta.getLore())
{
ret.add(new SimpleEntry<String, Integer>(line, PRIORITY_DEFAULT));
}
return ret;
}
// -------------------------------------------- //
// UTILITY SORT
// -------------------------------------------- //
public void setPriorityByPredicate(Predicate<String> predicate, int priority)
{
// Look over all the lore ...
for (Entry<String, Integer> loreEntry : this.getLore())
{
String line = loreEntry.getKey();
// ... and if predicate matches ...
if ( ! predicate.apply(line)) continue;
// ... set priority.
loreEntry.setValue(priority);
}
}
public void setPriorityByPrefix(final String prefix, int priority)
{
Predicate<String> predicate = new Predicate<String>()
{
@Override public boolean apply(String type)
{
return type.startsWith(prefix);
}
};
this.setPriorityByPredicate(predicate, priority);
}
public void setPriorityByRegex(final Pattern pattern, int priority)
{
Predicate<String> predicate = new Predicate<String>()
{
@Override public boolean apply(String type)
{
return pattern.matcher(type).matches();
}
};
this.setPriorityByPredicate(predicate, priority);
}
public void setPriorityByRegex(final String regex, int priority)
{
setPriorityByRegex(Pattern.compile(regex), priority);
}
}

View File

@ -1,12 +1,13 @@
package com.massivecraft.massivecore.util;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -27,6 +28,9 @@ import org.bukkit.material.MaterialData;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.comparator.ComparatorComparable;
import com.massivecraft.massivecore.comparator.ComparatorEntryValue;
import com.massivecraft.massivecore.event.EventMassiveCoreLorePriority;
import com.massivecraft.massivecore.mixin.MixinInventory;
public class InventoryUtil
@ -1194,4 +1198,58 @@ public class InventoryUtil
setLore(item, Arrays.asList(lore));
}
public static void addLore(ItemStack item, Collection<String> lore)
{
List<String> lines = getLore(item);
if (lines == null) lines = new MassiveList<>();
lines.addAll(lore);
InventoryUtil.setLore(item, lines);
}
public static void addLore(ItemStack item, String... lore)
{
addLore(item, Arrays.asList(lore));
}
// -------------------------------------------- //
// SORT LORE
// -------------------------------------------- //
public static List<String> getSortedLore(ItemStack item)
{
if ( ! item.getItemMeta().hasLore()) return Collections.emptyList();
EventMassiveCoreLorePriority event = new EventMassiveCoreLorePriority(item);
event.run();
List<Entry<String, Integer>> entries = event.getLore();
// Note: Comparator cast is necessary for Maven to compile, even if the IDE doesn't complain.
Comparator<Entry<? super String, ? super Integer>> comparator = (Comparator) ComparatorEntryValue.get(ComparatorComparable.get());
Collections.sort(entries, comparator);
List<String> ret = new MassiveList<>();
for (Entry<String, Integer> entry : entries)
{
ret.add(entry.getKey());
}
return ret;
}
public static void sortLore(ItemStack item)
{
if (item == null) return;
if ( ! item.getItemMeta().hasLore()) return;
List<String> lore = getSortedLore(item);
InventoryUtil.setLore(item, lore);
}
public static void sortLore(Iterable<ItemStack> items)
{
if (items == null) return;
for (ItemStack item : items)
{
sortLore(item);
}
}
}