mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-30 09:49:51 +02:00
ItemDb now loads in half the time, uses 72KB less memory.
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import lombok.Cleanup;
|
import lombok.Cleanup;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
@@ -22,9 +23,9 @@ public class ItemDb implements IItemDb
|
|||||||
this.ess = ess;
|
this.ess = ess;
|
||||||
file = new ManagedFile("items.csv", ess);
|
file = new ManagedFile("items.csv", ess);
|
||||||
}
|
}
|
||||||
private final transient Map<String, Integer> items = new HashMap<String, Integer>();
|
private final transient Map<String, Long> items = new HashMap<String, Long>();
|
||||||
private final transient Map<String, Short> durabilities = new HashMap<String, Short>();
|
|
||||||
private final transient ManagedFile file;
|
private final transient ManagedFile file;
|
||||||
|
private static final Pattern SPLIT = Pattern.compile("[^a-zA-Z0-9]");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReload()
|
public void onReload()
|
||||||
@@ -36,27 +37,26 @@ public class ItemDb implements IItemDb
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
durabilities.clear();
|
|
||||||
items.clear();
|
items.clear();
|
||||||
|
|
||||||
for (String line : lines)
|
for (String line : lines)
|
||||||
{
|
{
|
||||||
line = line.trim().toLowerCase(Locale.ENGLISH);
|
line = line.trim();
|
||||||
if (line.length() > 0 && line.charAt(0) == '#')
|
if (line.length() > 0 && line.charAt(0) == '#')
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String[] parts = line.split("[^a-z0-9]");
|
final String[] parts = SPLIT.split(line);
|
||||||
if (parts.length < 2)
|
if (parts.length < 2)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final int numeric = Integer.parseInt(parts[1]);
|
final long numeric = Integer.parseInt(parts[1]);
|
||||||
|
|
||||||
durabilities.put(parts[0].toLowerCase(Locale.ENGLISH), parts.length > 2 && !parts[2].equals("0") ? Short.parseShort(parts[2]) : 0);
|
final long durability = parts.length > 2 && !(parts[2].length() == 1 && parts[2].charAt(0) == '0') ? Short.parseShort(parts[2]) : 0;
|
||||||
items.put(parts[0].toLowerCase(Locale.ENGLISH), numeric);
|
items.put(parts[0].toLowerCase(Locale.ENGLISH), numeric | (durability << 32));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,24 +64,24 @@ public class ItemDb implements IItemDb
|
|||||||
{
|
{
|
||||||
final ItemStack stack = get(id.toLowerCase(Locale.ENGLISH));
|
final ItemStack stack = get(id.toLowerCase(Locale.ENGLISH));
|
||||||
|
|
||||||
int defaultStackSize = 0;
|
|
||||||
int oversizedStackSize = 0;
|
|
||||||
@Cleanup
|
@Cleanup
|
||||||
com.earth2me.essentials.api.ISettings settings = ess.getSettings();
|
com.earth2me.essentials.api.ISettings settings = ess.getSettings();
|
||||||
settings.acquireReadLock();
|
settings.acquireReadLock();
|
||||||
|
|
||||||
defaultStackSize = settings.getData().getGeneral().getDefaultStacksize();
|
final int defaultStackSize = settings.getData().getGeneral().getDefaultStacksize();
|
||||||
oversizedStackSize = settings.getData().getGeneral().getOversizedStacksize();
|
|
||||||
|
|
||||||
if (defaultStackSize > 0)
|
if (defaultStackSize > 0)
|
||||||
{
|
{
|
||||||
stack.setAmount(defaultStackSize);
|
stack.setAmount(defaultStackSize);
|
||||||
}
|
}
|
||||||
else if (oversizedStackSize > 0 && user.isAuthorized("essentials.oversizedstacks"))
|
else
|
||||||
|
{
|
||||||
|
final int oversizedStackSize = settings.getData().getGeneral().getOversizedStacksize();
|
||||||
|
if (oversizedStackSize > 0 && user.isAuthorized("essentials.oversizedstacks"))
|
||||||
{
|
{
|
||||||
stack.setAmount(oversizedStackSize);
|
stack.setAmount(oversizedStackSize);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,10 +120,11 @@ public class ItemDb implements IItemDb
|
|||||||
{
|
{
|
||||||
if (items.containsKey(itemname))
|
if (items.containsKey(itemname))
|
||||||
{
|
{
|
||||||
itemid = items.get(itemname);
|
long item = items.get(itemname);
|
||||||
if (durabilities.containsKey(itemname) && metaData == 0)
|
itemid = (int)(item & 0xffffffffL);
|
||||||
|
if (metaData == 0)
|
||||||
{
|
{
|
||||||
metaData = durabilities.get(itemname);
|
metaData = (short)((item >> 32) & 0xffffL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (Material.getMaterial(itemname) != null)
|
else if (Material.getMaterial(itemname) != null)
|
||||||
|
Reference in New Issue
Block a user