mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-08-05 20:12:54 +00:00
Optimize TextInput to cache motd and info textfiles.
This commit is contained in:
parent
81ec87d893
commit
f26cccb663
2 changed files with 62 additions and 28 deletions
|
@ -4,6 +4,7 @@ import com.earth2me.essentials.DescParseTickFormat;
|
||||||
import com.earth2me.essentials.IEssentials;
|
import com.earth2me.essentials.IEssentials;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -16,15 +17,17 @@ import org.bukkit.plugin.Plugin;
|
||||||
public class KeywordReplacer implements IText
|
public class KeywordReplacer implements IText
|
||||||
{
|
{
|
||||||
private final transient IText input;
|
private final transient IText input;
|
||||||
|
private final transient List<String> replaced;
|
||||||
private final transient IEssentials ess;
|
private final transient IEssentials ess;
|
||||||
|
|
||||||
public KeywordReplacer(final IText input, final CommandSender sender, final IEssentials ess)
|
public KeywordReplacer(final IText input, final CommandSender sender, final IEssentials ess)
|
||||||
{
|
{
|
||||||
this.input = input;
|
this.input = input;
|
||||||
|
this.replaced = new ArrayList<String>(this.input.getLines().size());
|
||||||
this.ess = ess;
|
this.ess = ess;
|
||||||
replaceKeywords(sender);
|
replaceKeywords(sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void replaceKeywords(final CommandSender sender)
|
private void replaceKeywords(final CommandSender sender)
|
||||||
{
|
{
|
||||||
String displayName, ipAddress, balance, mails, world;
|
String displayName, ipAddress, balance, mails, world;
|
||||||
|
@ -98,7 +101,7 @@ public class KeywordReplacer implements IText
|
||||||
|
|
||||||
date = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date());
|
date = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date());
|
||||||
time = DateFormat.getTimeInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date());
|
time = DateFormat.getTimeInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date());
|
||||||
|
|
||||||
version = ess.getServer().getVersion();
|
version = ess.getServer().getVersion();
|
||||||
|
|
||||||
for (int i = 0; i < input.getLines().size(); i++)
|
for (int i = 0; i < input.getLines().size(); i++)
|
||||||
|
@ -120,14 +123,14 @@ public class KeywordReplacer implements IText
|
||||||
line = line.replace("{WORLDDATE}", worldDate);
|
line = line.replace("{WORLDDATE}", worldDate);
|
||||||
line = line.replace("{PLUGINS}", plugins);
|
line = line.replace("{PLUGINS}", plugins);
|
||||||
line = line.replace("{VERSION}", version);
|
line = line.replace("{VERSION}", version);
|
||||||
input.getLines().set(i, line);
|
replaced.add(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getLines()
|
public List<String> getLines()
|
||||||
{
|
{
|
||||||
return input.getLines();
|
return replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.earth2me.essentials.IEssentials;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.Util;
|
import com.earth2me.essentials.Util;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.lang.ref.SoftReference;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
@ -11,9 +12,11 @@ import org.bukkit.entity.Player;
|
||||||
|
|
||||||
public class TextInput implements IText
|
public class TextInput implements IText
|
||||||
{
|
{
|
||||||
private final transient List<String> lines = new ArrayList<String>();
|
private final transient List<String> lines;
|
||||||
private final transient List<String> chapters = new ArrayList<String>();
|
private final transient List<String> chapters;
|
||||||
private final transient Map<String, Integer> bookmarks = new HashMap<String, Integer>();
|
private final transient Map<String, Integer> bookmarks;
|
||||||
|
private final transient long lastChange;
|
||||||
|
private final static HashMap<String, SoftReference<TextInput>> cache = new HashMap<String, SoftReference<TextInput>>();
|
||||||
|
|
||||||
public TextInput(final CommandSender sender, final String filename, final boolean createFile, final IEssentials ess) throws IOException
|
public TextInput(final CommandSender sender, final String filename, final boolean createFile, final IEssentials ess) throws IOException
|
||||||
{
|
{
|
||||||
|
@ -34,33 +37,62 @@ public class TextInput implements IText
|
||||||
}
|
}
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
{
|
{
|
||||||
final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
lastChange = file.lastModified();
|
||||||
try
|
boolean readFromfile;
|
||||||
|
synchronized (cache)
|
||||||
{
|
{
|
||||||
int lineNumber = 0;
|
final SoftReference<TextInput> inputRef = cache.get(file.getName());
|
||||||
while (bufferedReader.ready())
|
TextInput input;
|
||||||
|
if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange)
|
||||||
{
|
{
|
||||||
final String line = bufferedReader.readLine();
|
lines = new ArrayList<String>();
|
||||||
if (line == null)
|
chapters = new ArrayList<String>();
|
||||||
{
|
bookmarks = new HashMap<String, Integer>();
|
||||||
break;
|
cache.put(file.getName(), new SoftReference<TextInput>(this));
|
||||||
}
|
readFromfile = true;
|
||||||
if (line.length() > 0 && line.charAt(0) == '#')
|
}
|
||||||
{
|
else
|
||||||
bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber);
|
{
|
||||||
chapters.add(line.substring(1).replace('&', '§').replace("§§", "&"));
|
lines = Collections.unmodifiableList(input.getLines());
|
||||||
}
|
chapters = Collections.unmodifiableList(input.getChapters());
|
||||||
lines.add(line.replace('&', '§').replace("§§", "&"));
|
bookmarks = Collections.unmodifiableMap(input.getBookmarks());
|
||||||
lineNumber++;
|
readFromfile = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
if (readFromfile)
|
||||||
{
|
{
|
||||||
bufferedReader.close();
|
final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int lineNumber = 0;
|
||||||
|
while (bufferedReader.ready())
|
||||||
|
{
|
||||||
|
final String line = bufferedReader.readLine();
|
||||||
|
if (line == null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (line.length() > 0 && line.charAt(0) == '#')
|
||||||
|
{
|
||||||
|
bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber);
|
||||||
|
chapters.add(line.substring(1).replace('&', '§').replace("§§", "&"));
|
||||||
|
}
|
||||||
|
lines.add(line.replace('&', '§').replace("§§", "&"));
|
||||||
|
lineNumber++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
bufferedReader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
lastChange = 0;
|
||||||
|
lines = Collections.emptyList();
|
||||||
|
chapters = Collections.emptyList();
|
||||||
|
bookmarks = Collections.emptyMap();
|
||||||
if (createFile)
|
if (createFile)
|
||||||
{
|
{
|
||||||
final InputStream input = ess.getResource(filename + ".txt");
|
final InputStream input = ess.getResource(filename + ".txt");
|
||||||
|
@ -68,8 +100,7 @@ public class TextInput implements IText
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
final byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
int length = 0;
|
int length = input.read(buffer);
|
||||||
length = input.read(buffer);
|
|
||||||
while (length > 0)
|
while (length > 0)
|
||||||
{
|
{
|
||||||
output.write(buffer, 0, length);
|
output.write(buffer, 0, length);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue