Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
New regular expression code. Fixes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
bziemons committed Jan 8, 2014
1 parent 56527b6 commit 4af9c95
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/thezorro266/bukkit/srm/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void onPlayerJoin(PlayerJoinEvent event) {
OwnableTemplate ownableTemplate = (OwnableTemplate) template;
synchronized (template.getRegionList()) {
for (RegionFactory.Region region : template.getRegionList()) {
if (ownableTemplate.getMainOwner(region).equalsIgnoreCase(event.getPlayer().getName())) {
String mainOwner = ownableTemplate.getMainOwner(region);
if (mainOwner != null && mainOwner.equalsIgnoreCase(event.getPlayer().getName())) {
tempRegions.add(region);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.thezorro266.bukkit.srm.templates;

import static com.thezorro266.bukkit.srm.factories.SignFactory.Sign.SIGN_LINE_COUNT;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -51,53 +52,52 @@ public SignTemplate(ConfigurationSection templateConfigSection) {
}

private static String replaceTokens(String text, Map<String, String> replacementMap) {
final Pattern pattern = Pattern.compile("\\[\\[(.+?)\\]\\]");
final Matcher matcher = pattern.matcher(text);
final StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
try {
final String replacement = replacementMap.get(matcher.group(1));
if (replacement != null) {
matcher.appendReplacement(buffer, "");
buffer.append(replacement);
}
} catch (Throwable e) {
throw new RuntimeException("Replacement map has a misconfiguration at " + matcher.group(1), e);
Matcher matcher = Pattern.compile("(\\[\\[\\w+\\]\\])").matcher(text);
if (matcher.find()) {
ArrayList<SimpleEntry<Integer, String>> entryList = new ArrayList<SimpleEntry<Integer, String>>();
do {
entryList.add(new SimpleEntry<Integer, String>(matcher.end(), matcher.group()));
} while (matcher.find());

for (int i = entryList.size() - 1; i >= 0; --i) {
SimpleEntry<Integer, String> entry = entryList.get(i);
StringBuilder newText = new StringBuilder();
newText.append(text.substring(0, entry.getKey() - entry.getValue().length()));
newText.append(replacementMap.get(entry.getValue().substring(2, entry.getValue().length() - 2)));
newText.append(text.substring(entry.getKey()));
text = newText.toString();
}
}
matcher.appendTail(buffer);
return buffer.toString();
return text;
}

private static HashMap<String, String> getSignInput(SignTemplate signTemplate, String[] lines) {
final HashMap<String, String> outputMap = new HashMap<String, String>();
HashMap<String, String> outputMap = new HashMap<String, String>();

for (int i = 0; i < SIGN_LINE_COUNT; i++) {
final String inputLine = signTemplate.signInput[i];
String inputLine = signTemplate.signInput[i];
if (inputLine != null && !inputLine.isEmpty()) {

Pattern pattern = Pattern.compile("\\[\\[(.+?)\\]\\]");
Matcher matcher = pattern.matcher(inputLine);
final ArrayList<String> keys = new ArrayList<String>();
while (matcher.find()) {
for (int u = 0; u < matcher.groupCount(); u++) {
keys.add(matcher.group(u + 1));
}
}
String newPattern = matcher.replaceAll("(.+)");

pattern = Pattern.compile(newPattern);
matcher = pattern.matcher(lines[i]);
final ArrayList<String> vars = new ArrayList<String>();
while (matcher.find()) {
for (int u = 0; u < matcher.groupCount(); u++) {
vars.add(matcher.group(u + 1));
}
}
for (int u = 0; u < keys.size(); u++) {
if (u < vars.size()) {
outputMap.put(keys.get(u), vars.get(u));
} else {
outputMap.put(keys.get(u), null);
Matcher matcher = Pattern.compile("(\\[\\[\\w+\\]\\])").matcher(inputLine);

if (matcher.find()) {
ArrayList<String> paramList = new ArrayList<String>();
do {
String param = matcher.group();
paramList.add(param.substring(2, param.length() - 2));
} while (matcher.find());

String newPattern = String.format("\\Q%s\\E", matcher.replaceAll("\\\\E(.*)\\\\Q")).replaceAll(
"\\\\Q\\\\E", "");

int matchCount = 0;
matcher = Pattern.compile(newPattern).matcher(lines[i]);
if (matcher.matches()) {
for (int j = 1; j <= matcher.groupCount(); ++j) {
if (!matcher.group(j).isEmpty()) {
outputMap.put(paramList.get(matchCount), matcher.group(j));
}
++matchCount;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

package com.thezorro266.bukkit.srm.templates;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.thezorro266.bukkit.srm.LanguageSupport;
import com.thezorro266.bukkit.srm.SimpleRegionMarket;
Expand All @@ -27,16 +36,6 @@
import com.thezorro266.bukkit.srm.factories.SignFactory;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;

public class TemplateSell extends OwnableRegionTemplate {
protected double priceMin = 0;
Expand Down Expand Up @@ -110,7 +109,8 @@ public void regionCommand(Region region, CommandSender sender, String[] args) {
&& regionReset) {
try {
SimpleRegionMarket.getInstance().getWorldEditManager().saveRegionToSchematic(region);
sender.sendMessage(MessageFormat.format(LanguageSupport.instance.getString("region.schematic.save.successful"), region.getName()));
sender.sendMessage(MessageFormat.format(
LanguageSupport.instance.getString("region.schematic.save.successful"), region.getName()));
} catch (IOException e) {
sender.sendMessage(LanguageSupport.instance.getString("region.schematic.save.failure"));
SimpleRegionMarket
Expand Down Expand Up @@ -154,9 +154,9 @@ public void clickSign(Player player, Sign sign) {
// TODO: Player money
clearRegion(region);
if (buyerIsOwner) {
setRegionOwners(region, new OfflinePlayer[]{player});
setRegionOwners(region, new OfflinePlayer[] { player });
} else {
setRegionMembers(region, new OfflinePlayer[]{player});
setRegionMembers(region, new OfflinePlayer[] { player });
}

region.getOptions().set("buyer", player.getName());
Expand Down

0 comments on commit 4af9c95

Please sign in to comment.